package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.UserService1;
import model.User;
import model.UserPage;
import model.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.UserService;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;


@Controller

public class userController {

    @Autowired
    private UserService userService;

    @Autowired
    private User user;

    @Autowired
    private Users users;

    @Autowired
    private UserPage userPage;

    @RequestMapping("/userList")
    public String sendToUser() {

        return "userList";
    }

    //完成mybatis
    @RequestMapping("/add")
    public String add() {
        return "forward:/WEB-INF/pages/Add.jsp";
    }

    @RequestMapping(value = "/addData", produces = "text/html;charset=UTF-8;")
    @ResponseBody
    public String addData(Users user, HttpServletResponse response) {

        int rs = 0;
        rs = userService.saveUser(user);
        if (rs != 0) {
            String m = "添加成功";
            return m;
        } else {
            String l = "添加失败，id已存在";
            return l;
        }
    }

    //删除数据可以使用 完成mybatis
    @RequestMapping("/delUsers")
    @ResponseBody
    public UserPage delUsers(HttpServletRequest request, HttpServletResponse response) {

        List<Users> userss = new ArrayList<Users>();
        if (!userss.isEmpty()) {
            userss.clear();
//            System.out.println(users);
        }
        String id = request.getParameter("id");
        String userName = request.getParameter("userName");

        int totalCount = 0; //总数目

        int pageSize = 2; //页面几条数据
        int totalPage = 0;//页数
        String currentPage = request.getParameter("currentPage");
        System.out.println(currentPage);
        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }


        int rs = userService.delUser(id);
        if (rs != 0) {
            //删除成功，刷新数据
            userService.countUser(userName);
        }
        totalPage = totalCount % pageSize > 0 ? totalCount / pageSize + 1 : totalCount / pageSize;
        int beginIndex = (Integer.parseInt(currentPage) - 1) * pageSize;
        userss = (userService.findUser(user.setUserName(userName).setBeginIndex(beginIndex).setPageSize(pageSize)));

        UserPage userPage = new UserPage();
        userPage.setCurrentPage(Integer.parseInt(currentPage));
        userPage.setTotalCount(totalCount);
        userPage.setUsersList(userss);
        userPage.setTotalPage(totalPage);

        return userPage;
    }

    //可以使用 完成mybatis
    @RequestMapping("/det")
    public String det(HttpServletRequest request) {

        String id = request.getParameter("id");

        users = userService.userById(id);

        request.setAttribute("user", users);
        return "forward:/WEB-INF/pages/Det.jsp";
    }

    //搜索数据可以使用，mybatis完成
    @RequestMapping("/search")
    @ResponseBody
    public UserPage search(String userName, String currentPage) {
        List<Users> userss = new ArrayList<Users>();
        if (!userss.isEmpty()) {
            userss.clear();
//			System.out.println(users);
        }

//        模糊搜索;
        String userName1 = "%" + userName + "%";
        int totalCount = 0; //总数目
        int pageSize = 2; //页面几条数据
        int totalPage = 0;//页数

//		System.out.println(currentPage);
        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }

        //查询总数
        totalCount = userService.countUser(userName1);

        totalPage = totalCount % pageSize > 0 ? totalCount / pageSize + 1 : totalCount / pageSize;
        int beginIndex = (Integer.parseInt(currentPage) - 1) * pageSize;

        userss = (userService.findUser(user.setUserName(userName1).setBeginIndex(beginIndex).setPageSize(pageSize)));

        userPage.setCurrentPage(Integer.parseInt(currentPage));
        userPage.setTotalCount(totalCount);
        userPage.setUsersList(userss);
        userPage.setTotalPage(totalPage);

//        System.out.println(userPage);
        return userPage;
    }

    @RequestMapping("/servletChange")
    public String servletChange() {
        return "";
    }

    //可以使用
    @RequestMapping("/upt")
    public String upt(HttpServletRequest request) {

        String id = request.getParameter("idq");
        String userName = request.getParameter("userName");
        String sex = request.getParameter("sex");
        String age = request.getParameter("age");
        String addr = request.getParameter("addr");
        String qq = request.getParameter("qq");
        String email = request.getParameter("email");
        users.setId(id);
        users.setAddr(addr);
        users.setUserName(userName);
        users.setAge(age);
        users.setSex(sex);
        users.setQq(qq);
        users.setEmail(email);
        int rs = 0;
        rs = userService.updataUser(users);

        return "userList";
    }

    //可以使用
    @RequestMapping("/uptPage")
    public String uptPage(HttpServletRequest request) {


        String id = request.getParameter("id");
        users = userService.userById(id);
        request.setAttribute("user", users);

        return "upPage";
    }
}
